var _d2hToken = /[^\s,;]+/g; var _d2hMaxFraction = 16; function _d2hOpt(id, fallback) { var el = document.getElementById(id); return el && el.value !== undefined && el.value !== '' ? el.value : fallback; } function _d2hParse(token) { var t = token.replace(/_/g, ''); var m = /^([-+]?)(\d*)(?:\.(\d*))?$/.exec(t); if (!m || !(m[2] + (m[3] || '')).length) return null; return { negative: m[1] === '-', whole: BigInt(m[2] || '0'), fraction: (m[3] || '').replace(/0+$/, '') }; } // Multiply the decimal fraction by 16 one hex digit at a time, exactly, until it ends or hits the cap. function _d2hFraction(dec) { if (!dec) return { text: '', cut: false }; var scale = 10n ** BigInt(dec.length), n = BigInt(dec), out = ''; while (n > 0n && out.length < _d2hMaxFraction) { n *= 16n; out += (n / scale).toString(16); n %= scale; } return { text: '.' + out, cut: n > 0n }; } function _d2hHex(p, opts) { var frac = _d2hFraction(p.fraction); var isNegative = p.negative && (p.whole !== 0n || p.fraction); var digits, sign = '', twos = 0; if (isNegative && opts.negative === 'twos' && !p.fraction) { // Smallest common width that holds the value. var w = 8; while (p.whole > (1n << BigInt(w - 1))) w *= 2; twos = w; digits = ((1n << BigInt(w)) - p.whole).toString(16).padStart(w / 4, '0'); } else { digits = p.whole.toString(16) + frac.text; if (isNegative) sign = '-'; } if (opts.upper) digits = digits.toUpperCase(); return { text: sign + opts.prefix + digits, cut: frac.cut, twos: twos, fractionTwos: isNegative && opts.negative === 'twos' && !!p.fraction }; } function _d2hGroup(s) { return s.replace(/\B(?=(\d{4})+(?!\d))/g, ' '); } function convert(input) { var status = document.getElementById('d2h-status'); var opts = { upper: _d2hOpt('d2h-case', 'upper') === 'upper', prefix: _d2hOpt('d2h-prefix', ''), negative: _d2hOpt('d2h-negative', 'minus') }; var text = input || ''; var note = ''; // One number written with thousands separators (1,000,000 or 1,00,000) rather than a list. var trimmed = text.trim(); if (trimmed.indexOf(',') > 0 && /^[-+]?(\d{1,3}(,\d{3})+|\d{1,2}(,\d{2})*,\d{3})(\.\d+)?$/.test(trimmed)) { note = ' Read ' + trimmed + ' as one number. Put spaces between numbers to convert a list.'; text = text.replace(trimmed, trimmed.replace(/,/g, '')); } var good = [], bad = [], cut = false, fractionTwos = false; var out = text.replace(_d2hToken, function (token) { var p = _d2hParse(token); if (!p) { bad.push(token); return '?'; } var h = _d2hHex(p, opts); good.push({ p: p, h: h }); cut = cut || h.cut; fractionTwos = fractionTwos || h.fractionTwos; return h.text; }); if (status) { var msg = ''; if (good.length === 1 && !bad.length && !good[0].p.fraction) { var g = good[0]; if (g.h.twos) msg = 'Signed ' + g.h.twos + '-bit two\'s complement.'; else msg = 'Binary ' + (g.p.negative && g.p.whole !== 0n ? '-' : '') + _d2hGroup(g.p.whole.toString(2)) + ', octal ' + (g.p.negative && g.p.whole !== 0n ? '-' : '') + g.p.whole.toString(8) + '.'; } else if (good.length > 1) msg = 'Converted ' + good.length + ' numbers.'; if (cut) msg += ' Fractions that never end in hex are cut off after ' + _d2hMaxFraction + ' hex digits.'; if (fractionTwos) msg += ' Two\'s complement only applies to whole numbers, so negative fractions keep a minus sign.'; msg += note; if (bad.length) { var shown = bad.slice(0, 3).map(function (t) { return t.length > 12 ? t.slice(0, 12) + '...' : t; }).join(', '); msg += ' ' + (bad.length === 1 ? 'This is' : bad.length + ' values are') + ' not a decimal number (shown as ?): ' + shown + (bad.length > 3 ? ', ...' : '') + '.'; } status.textContent = msg.trim(); } return out; } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }